package com.example.designpatterns.singleton;

/**
 * lazy loading
 * 进阶版：线程安全
 * 加synchronized ,效率下降
 */
public class Mgr03 {
    private static Mgr03 Instance;

    private Mgr03() {
    }

    public static synchronized Mgr03 getInstance() {
        if (Instance == null) {
            try {
                Thread.sleep(1);
            } catch (Exception e) {
                e.printStackTrace();
            }
            Instance = new Mgr03();
        }
        return Instance;
    }

    public static void main(String[] args) {
        for (int i = 0; i < 100; i++) {
            new Thread(() -> {
                System.out.println(Mgr03.getInstance().hashCode());
            }).start();
        }
    }
}
